import Vue from 'vue'
import Vuex from 'vuex'
import api from '@/api/index'
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		user_info: uni.getStorageSync('user_info') || {}, // 用户信息
		select_assets: uni.getStorageSync('select_assets') || {}, // 选择房间
	},
	getters: {},
	mutations: {
		// 设置用户信息
		SET_USER(state, info) {
			state.user_info = info
			uni.setStorageSync('user_info', info)
		},
		// 清除用户信息
		CLEAR_USER(state) {
			state.user_info = {}
			state.select_assets = {}
			uni.removeStorageSync('user_info')
			uni.removeStorageSync('access_token')
			uni.removeStorageSync('select_assets')
			uni.removeStorageSync('member_id')
		},
		// 选择房间信息（合同）
		SET_SELECT_ASSETS(state, info) {
			state.select_assets = info
			uni.setStorageSync('select_assets', info)
		},
	},
	actions: {
		// 获取用户信息
		async getUserInfo({
			commit
		}) {
			return new Promise(async (resolve) => {
				const res = await api.common.user('getUserInfo')
				if (res.code === 200) {
					commit('SET_USER', res.data)
				} else {
					commit('CLEAR_USER')
				}
				resolve(res)
			})
		},
	},
})
export default store
